
package BallGame2;

//抽象窗口工具箱 重量级
import java.awt.*;
//图形可视包 轻量级
import javax.swing.*;

public class BallGame2 extends JFrame{

    Image ball=Toolkit.getDefaultToolkit().getImage("image/ball.png");
    Image desk=Toolkit.getDefaultToolkit().getImage("image/desk.png");

    double x=100;
    double y=100;
    double degree=3.14/3;//弧度,就是60度

    public void paint(Graphics g){
        System.out.println("窗口被调用了!");
        g.drawImage(desk,0,30,null);
        g.drawImage(ball,(int)x,(int)y,null);

        x=x+10*Math.cos(degree);
        y=y+10*Math.sin(degree);
        //碰到上下桌子改变角度
        if(y>400){
            degree=-degree;
        }
        if(y<70){
            degree=-degree;
        }
        //碰到左右桌子改变角度
        if(x<50||x>856-70){
            degree=3.14-degree;
        }
    }

    //窗口加载
    void launchFrame(){
        setSize(856,470);
        setLocation(50,50);
        setVisible(true);

        //重画窗口
        while (true){
            repaint();
            try{
                Thread.sleep(40);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    //main方法是程序执行的入口
    public static void main(String[] args) {
        BallGame2 game=new BallGame2();
        game.launchFrame();

    }
}

